package org.ichow.scenegame.notis
{
	import flash.display.DisplayObject;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import org.ichow.scenegame.core.Item;
	import org.ichow.scenegame.events.NotiEvent;
	import org.ichow.scenegame.events.SceneEvent;
	import org.ichow.debug.Log;
	/**
	 * ...
	 * @author M.ichow
	 */
	public class OptionsNoti extends BaseNoti
	{
		private var _item:Class;
		//选项数组
		private var _itemsAry:Array;
		//路线储存
		private var _ways:Array;
		//每页数量
		private var page:int = 4;
		//当前选项数组
		private var _currItems:Array;
		//当前页
		private var currPage:int = 0;
		//当前选项
		private var currItem:Item;
		//最大页
		private var _maxPage:Number;
		//按钮
		private var __okBt:SimpleButton;
		private var __nextBt:SimpleButton;
		private var __preBt:SimpleButton;
		//信息文本
		private var __infoTf:TextField;
		//选项容器
		private var __itemsMc:Sprite;
		//场景数据
		private var _data:XML;
		
		public function OptionsNoti()
		{
			super();
		}
		
		override protected function init():void
		{
			super.init();
			_itemsAry = [];
			_ways = [];
			
			var skin:Sprite = _noti as Sprite;
			__infoTf = skin.getChildByName( "info_txt" ) as TextField;
			__preBt = skin.getChildByName( "pre_btn" ) as SimpleButton;
			__nextBt = skin.getChildByName( "next_btn" ) as SimpleButton;
			__okBt = skin.getChildByName( "ok_btn" ) as SimpleButton;
			__itemsMc = skin.getChildByName( "items_mc" ) as Sprite;
			
			__okBt.addEventListener( MouseEvent.CLICK , onItemsOkMouseClick );
			__preBt.addEventListener( MouseEvent.CLICK , onItemsPreMouseClick );
			__nextBt.addEventListener( MouseEvent.CLICK , onItemsNextMouseClick );
		
		}
		
		override public function update( o:Object = null ):void
		{
			super.update( o );
			
			__infoTf.text = _data.@info;
			currItems = _data.@init.split( "," );
		}
		
		override public function reset():void
		{
			super.reset();
			_ways = [];
			//
			for ( var i:int = 0 , len:int = _itemsAry.length ; i < len ; i++ )
			{
				var item:Item = _itemsAry[ i ];
				if ( item )
					item.count = 0;
			}
			//
			__infoTf.text = _data.@info;
			currItems = _data.@init.split( "," );
		}
		
		/**
		 * 选项提交
		 * @param	e
		 */
		private function onItemsOkMouseClick( e:MouseEvent ):void
		{
			if ( !currItem )
			{
				Log.trace( "先选择!" );
				return;
			}
			// save 
			_ways.push( currItem );
			var evt:NotiEvent = new NotiEvent( NotiEvent.SELECT_ITEM , currItem );
			dispatchEvent( evt );
			
			cancel();
			__infoTf.text = currItem.info;
			currItems = currItem.children;
		
		}
		
		/**
		 * 选项翻页
		 * @param	e
		 */
		private function onItemsNextMouseClick( e:MouseEvent ):void
		{
			if ( currPage < ( _maxPage - 1 ) )
			{
				currPage++;
				updateItems();
			}
		}
		
		private function onItemsPreMouseClick( e:MouseEvent ):void
		{
			if ( currPage > 0 )
			{
				currPage--;
				updateItems();
			}
		}
		
		/**
		 * 更新选项
		 */
		private function updateItems():void
		{
			while ( __itemsMc.numChildren > 0 )
			{
				__itemsMc.removeChildAt( 0 );
			}
			var start:int = currPage * page;
			var items:Array = currItems.slice( start , start + page );
			//
			Log.trace( "当前选项: " + items );
			for ( var i:int = 0 , len:int = items.length ; i < len ; i++ )
			{
				var item:Item = getItem( items[ i ] );
				item.y = 10;
				item.x = 10 + i * ( item.width + 5 );
				__itemsMc.addChild( item );
			}
			if ( currPage > 0 )
				__preBt.visible = true;
			else
				__preBt.visible = false;
			if ( currPage < ( _maxPage - 1 ) )
				__nextBt.visible = true;
			else
				__nextBt.visible = false;
		}
		
		/**
		 * 获取选项
		 * @param	id
		 * @return
		 */
		private function getItem( id:int ):Item
		{
			var m:Item = _itemsAry[ id ];
			if ( !m )
			{
				m = new Item( new _item() );
				var _xml:XML = _data.item[ id ];
				m.title = _xml.@title;
				m.id = _xml.@id;
				m.icon = _xml.@icon;
				m.url = _xml.@url;
				m.des = _xml.@des;
				m.info = _xml.@info;
				m.children = _xml.@children.split( "," );
				m.score = _xml.@score.split( "," );
				m.continueValue = _xml.continueValue;
				m.endValue = _xml.endValue;
				
				// set mouse event
				m.addEventListener( MouseEvent.CLICK , onItemsMouseClick );
				m.addEventListener( MouseEvent.ROLL_OVER , onItemsRollOver );
				m.addEventListener( MouseEvent.ROLL_OUT , onItemsRollOut );
				_itemsAry[ id ] = m;
			}
			return m;
		}
		
		private function onItemsRollOver( e:MouseEvent ):void
		{
			var item:Item = e.currentTarget as Item;
			dispatchEvent( new NotiEvent( NotiEvent.TOOLTIP , item ) );
		}
		
		private function onItemsRollOut( e:MouseEvent ):void
		{
			dispatchEvent( new NotiEvent( NotiEvent.TOOLTIP ) );
		}
		
		/**
		 *
		 * @param	e
		 */
		private function onItemsMouseClick( e:MouseEvent ):void
		{
			if ( currItem )
				currItem.clear();
			currItem = e.currentTarget as Item;
			currItem.draw();
		}
		
		/**
		 * 设置当前选项数组
		 */
		public function get currItems():Array
		{
			return _currItems;
		}
		
		/**
		 *
		 */
		public function set currItems( value:Array ):void
		{
			_currItems = value;
			//出现次数加1
			for ( var i:int = 0 , len:int = _currItems.length ; i < len ; i++ )
			{
				var item:Item = getItem( _currItems[ i ] );
				item.count++;
			}
			//最大页数
			_maxPage = Math.ceil( _currItems.length / page );
			currPage = 0;
			//清除焦点
			if ( currItem )
				currItem.clear();
			currItem = null;
			//显示*
			updateItems();
		}
		
		public function get item():Class
		{
			return _item;
		}
		
		public function set item( value:Class ):void
		{
			_item = value;
		}
		
		public function get data():XML
		{
			return _data;
		}
		
		public function set data( value:XML ):void
		{
			_data = value;
		}
		
		public function get ways():Array
		{
			return _ways;
		}
	
	}

}